import axios from '@/utils/request'
import { getStorage } from '@/utils/storage'
// 获取所有频道
export const getAllChannelsAPI = () => {
  return axios({
    url: '/v1_0/channels',
    methods: 'GET'
  })
}

// 用户登录注册
export const loginAPI = ({ mobile, code }) => axios({
  url: '/v1_0/authorizations',
  method: 'POST',
  data: {
    mobile,
    code
  }
})
// 获取所有用户频道
export const getUserChannelsAPI = () => axios({
  url: '/v1_0/user/channels'
})
// 获取新闻推荐
export const getAllArticleList = ({ channel_id, timestamp }) => axios({
  url: '/v1_0/articles',
  params: {
    channel_id,
    timestamp
  }
})
// 文章-反馈-不感兴趣
export const dislikeArticleAPI = ({ artId }) => axios({
  url: '/v1_0/article/dislikes',
  method: 'POST',
  data: {
    target: artId
  }
})
// 文章-反馈-反馈垃圾内容
export const reportArticleAPI = ({ artId, type }) => axios({
  url: '/v1_0/article/reports',
  method: 'POST',
  data: {
    target: artId,
    type: type,
    remark: '如果你想写'
  }
})
// 设置用户频道
export const updateChannelsAPI = ({ channels }) => axios({
  url: '/v1_0/user/channels',
  method: 'PUT',
  data: {
    channels
  }
})
// 删除用户频道
export const removeTheChannelAPI = ({ channelId }) => axios({
  url: `/v1_0/user/channels/${channelId}`,
  method: 'DELETE'
})
// 搜索建议列表
export const suggerListAPI = ({ keyWord }) => axios({
  url: '/v1_0/suggestion',
  params: {
    q: keyWord
  }
})
// 搜索后的新闻
export const searchResultAPI = ({ page = 1, per_page = 10, q }) => axios({
  url: '/v1_0/search',
  params: {
    page,
    per_page,
    q
  }
})
// 获取文章详情
export const detailAPI = ({ artId }) => axios({
  url: `/v1_0/articles/${artId}`
})
// 用户关注
export const userFollowAPI = ({ userId }) => axios({
  url: '/v1_0/user/followings',
  method: 'POST',
  data: {
    target: userId
  }
})
// 用户取消关注
export const userUnFollowAPI = ({ userId }) => axios({
  url: `/v1_0/user/followings/${userId}`,
  method: 'DELETE'
})
// 对文章点赞
export const likeArticleAPI = ({ artId }) => axios({
  url: '/v1_0/article/likings',
  method: 'POST',
  data: {
    target: artId
  }
})
// 对文章取消点赞
export const unLikeArticleAPI = ({ artId }) => axios({
  url: `/v1_0/article/likings/${artId}`,
  method: 'DELETE'
})
// 获取文章列表进行评论
export const commonListAPI = ({ id, offset = null, limit = 10 }) => axios({
  url: '/v1_0/comments',
  params: {
    type: 'a',
    source: id,
    offset,
    limit
  }
})
// 对文章列表的评论进行点赞
export const commonLikingAPI = ({ comId }) => axios({
  url: '/v1_0/comment/likings',
  method: 'POST',
  data: {
    target: comId
  }
})
// 对文章列表的评论进行取消点赞
export const commonDisLikingAPI = ({ comId }) => axios({
  url: `/v1_0/comment/likings/${comId}`,
  method: 'DELETE'
})
// 对文章进行评论和对文章的评论进行评论
export const commentSendAPI = ({ id, content, art_id = null }) => {
  const obj = {
    target: id,
    content
  }
  if (art_id !== null) {
    obj.art_id = art_id
  }
  return axios(
    {
      url: '/v1_0/comments',
      method: 'POST',
      data: obj
    })
}
// 用户 - 获取用户自己信息
export const getUserInfoAPI = () => axios({
  url: '/v1_0/user'
})
// 用户 - 获取用户个人资料
export const userProfileAPI = () => axios({
  url: '/v1_0/user/profile'
})
// 用户 - 编辑用户照片资料
export const updateUserPhotoAPI = (fd) => axios({
  url: '/v1_0/user/photo',
  method: 'PATCH',
  data: fd
})
// 用户 - 编辑用户个人资料
export const updateUserProfileAPI = (dataObj) => {
  const obj = {
    name: '',
    gender: 0,
    birthday: '',
    intro: ''
  }
  for (const props in obj) {
    if (dataObj[props] === undefined) {
      delete obj[props]
    } else {
      obj[props] = dataObj[props]
    }
  }
  return axios({
    url: '/v1_0/user/profile',
    method: 'PATCH',
    data: obj
  })
}
// 身份过期重新获取token
export const getNewTokenAPI = () => axios({
  url: '/v1_0/authorizations',
  method: 'PUT',
  headers: {
    Authorization: 'Bearer ' + getStorage('refresh_token')
  }
})
